package
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.core.RenderSupport;
	import starling.core.Starling;
	import starling.display.DisplayObject;

	public class Utils
	{
		public function Utils()
		{
		}
		
		/**
		 * snapshoot for some starling displayObject 
		 */
		public static function snapshootFromObject(target:DisplayObject):BitmapData
		{
			var resultRect : Rectangle = new Rectangle();
			target.getBounds( target, resultRect);
			resultRect.setTo(resultRect.x,resultRect.y,resultRect.width,resultRect.height);
			var result : BitmapData = new BitmapData( Starling.current.stage.stageWidth, Starling.current.stage.stageHeight, false, 0x666666 );
			
			var context : Context3D = Starling.context;
			
			var support : RenderSupport = new RenderSupport();
			
			RenderSupport.clear();
			
			support.setOrthographicProjection(0, 0, Starling.current.stage.stageWidth, Starling.current.stage.stageHeight );
			
			support.applyBlendMode(true);
			
			support.translateMatrix( -resultRect.x, -resultRect.y);
			
			support.pushMatrix();
			
			support.blendMode = target.blendMode;
			
			target.render( support, 1.0 );
			
			support.popMatrix();
			
			support.finishQuadBatch();
			
			context.drawToBitmapData( result );
			
			var croppedRes:BitmapData = new BitmapData(resultRect.width, resultRect.height, false, 0x666666);
			
			var rect:Rectangle=new Rectangle(0,0,resultRect.width,resultRect.height);
			
			croppedRes.copyPixels(result,rect , new Point(0,0));
			
			return croppedRes;
		}
	}
}